﻿using AutoDeployForSims.Entity;
using AutoDeployForSims.Model;
using AutoDeployForSims.Service;
using AutoDeployForSims.Utility;
using MyUtility.Utility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoDeployForSims.ViewModel
{
    public class SelectSimuToolWindowViewModel : ViewModelBase
    {
        private CallOutAppModel callOutAppModel = SingletonProvider<CallOutAppModel>.Instance;
        private AppInfoModel appInfoModel = SingletonProvider<AppInfoModel>.Instance;
        private AllStatusDisplayControlViewModel AllStatusDisplayControlVM = SingletonProvider<AllStatusDisplayControlViewModel>.Instance;
        private GetConfigService getConfigService = SingletonProvider<GetConfigService>.Instance;
        private FrankCFGInfoControlViewModel FrankCFGInfoControlVM = SingletonProvider<FrankCFGInfoControlViewModel>.Instance;
        private CANTotalInfoControlViewModel CANTotalInfoControlVM = SingletonProvider<CANTotalInfoControlViewModel>.Instance;


        List<AppStatusEntity> newAppStatusEntities = new List<AppStatusEntity>();

        #region ViewModel

        private ObservableCollection<SimuToolInfoEntity> simuToolInfoList = new ObservableCollection<SimuToolInfoEntity>();        
        public ObservableCollection<SimuToolInfoEntity> SimuToolInfoList
        {
            get { return simuToolInfoList; }
            set { simuToolInfoList = value; RaisePropertyChanged("SimuToolInfoList"); }
        }

        #endregion
        public SelectSimuToolWindowViewModel()
        {

        }

        public void OpenSimuTool()
        {
            //删除面板上相应的工具
            if (getConfigService.ReadNewAPPStartPathConfig(ref newAppStatusEntities))
            {
                int num = newAppStatusEntities.Count();
                int numSum = appInfoModel.AppStatusEntities.Count() - 1;
                for (int i = numSum; i > numSum - num; i--)
                {
                    AllStatusDisplayControlVM.DeleteStatusPanel(appInfoModel.AppStatusEntities[i]);
                    appInfoModel.DeleteApp(appInfoModel.AppStatusEntities[i]);                    
                }         
            }

            //存储到配置库
            //List<SimuToolInfoEntity> SimuToolInfoListTemp = new List<SimuToolInfoEntity>();
            if (getConfigService.WriteNewAPPStartPathConfigFile(SimuToolInfoList))
            {
                foreach (var item in SimuToolInfoList)
                {
                    if (item.IsNeed)
                    {
                        //增加到监控页面
                        AppStatusEntity appStatusEntity = new AppStatusEntity(item.SimuToolName, item.SimuAPPName, item.SimuAPPPath);
                        appInfoModel.AddApp(appStatusEntity);
                        AllStatusDisplayControlVM.UpdateStatusPanel(appStatusEntity);
                    }
                    if (item.IsStart)
                    {
                        //启动模拟工具
                        callOutAppModel.ShowApp(item.SimuAPPName, item.SimuAPPPath);
                    }
                }
            }
        }

        public void AnalysisFrankCFG()
        {
            
            SimuAppInfo CommInfo = new SimuAppInfo();
            List<SimuAppInfo> simuAppInfos = new List<SimuAppInfo>();
            List<FrankCFGInfoEntity> frankCFGInfos = new List<FrankCFGInfoEntity>();

            foreach (CommInfoControlViewMode item in FrankCFGInfoControlVM.Tabs)
            {
                FrankCFGInfoEntity frankCFGInfo = new FrankCFGInfoEntity();
                frankCFGInfo.CommIP = item.FrankCFGInfoEnt.CommIP;
                foreach (CommInfoEntity CommInfoEntity in item.FrankCFGInfoEnt.CommInfoEntities)
                {
                    //if (CommInfoEntity.IsStart)
                    {
                        CommInfoEntity commInfo = CommInfoEntity;
                        frankCFGInfo.CommInfoEntities.Add(commInfo);
                    }
                }
                frankCFGInfos.Add(frankCFGInfo);
            }
            
            if (frankCFGInfos.Count > 0)
            {
                if (getConfigService.ConfigCommBoard(frankCFGInfos, ref CommInfo))
                {
                    //添加虚拟通信板工具到列表
                    SimuToolInfoEntity simuToolInfoEntity = new SimuToolInfoEntity(SimuToolInfoList.Count + 1, "虚拟通信板", CommInfo.SimuAPPName, CommInfo.SimuAPPPath, true, false);
                    SimuToolInfoList.Add(simuToolInfoEntity);
                }
                if (getConfigService.ConfigModbusTool(frankCFGInfos, ref CommInfo))
                {
                    //添加Modbus模拟工具到列表
                    SimuToolInfoEntity simuToolInfoEntity = new SimuToolInfoEntity(SimuToolInfoList.Count + 1, "Modbus模拟", CommInfo.SimuAPPName, CommInfo.SimuAPPPath, true, false);
                    SimuToolInfoList.Add(simuToolInfoEntity);
                }
                if (getConfigService.ConfigWDWTool(frankCFGInfos, ref simuAppInfos))
                {
                    //添加外电网模拟工具到列表
                    int i = 0;
                    foreach (var simuAppInfo in simuAppInfos)
                    {
                        i++;
                        SimuToolInfoEntity simuToolInfoEntity = new SimuToolInfoEntity(SimuToolInfoList.Count + 1, "外电网模拟" + i.ToString(), simuAppInfo.SimuAPPName, simuAppInfo.SimuAPPPath, true, false);
                        SimuToolInfoList.Add(simuToolInfoEntity);
                    }
                }
                if (getConfigService.ConfigDCCurveTool(frankCFGInfos, ref simuAppInfos))
                {
                    //添加道岔曲线模拟工具到列表
                    int i = 0;
                    foreach (var simuAppInfo in simuAppInfos)
                    {
                        i++;
                        SimuToolInfoEntity simuToolInfoEntity = new SimuToolInfoEntity(SimuToolInfoList.Count + 1, "道岔曲线模拟" + i.ToString(), simuAppInfo.SimuAPPName, simuAppInfo.SimuAPPPath, true, false);
                        SimuToolInfoList.Add(simuToolInfoEntity);
                    }
                }
            }

            List<CANTotalInfoEntity> CANTotalInfos = new List<CANTotalInfoEntity>();
            //得到CAN相关数据
            foreach (CANInfoControlViewModel item in CANTotalInfoControlVM.CANTabs)
            {
                CANTotalInfoEntity cANTotalInfoEntity = new CANTotalInfoEntity();
                //根据ToolTypeName进行分类
                if (item.CANTotalInfoEnt.ToolTypeName != ToolTypeNames.type_Unknow)
                {
                    cANTotalInfoEntity = item.CANTotalInfoEnt;
                    CANTotalInfos.Add(cANTotalInfoEntity);
                }                
            }
            if (CANTotalInfos.Count > 0)
            {
                //修改虚拟通信版UDPCANResource.xml
                if (getConfigService.ConfigCommBoard_CAN(CANTotalInfos))
                {
                    if (getConfigService.ConfigJYLLTool(CANTotalInfos, ref CommInfo))
                    {
                        //添加电缆绝缘模拟工具到列表
                        SimuToolInfoEntity simuToolInfoEntity = new SimuToolInfoEntity(SimuToolInfoList.Count + 1, "电缆绝缘模拟", CommInfo.SimuAPPName, CommInfo.SimuAPPPath, true, false);
                        SimuToolInfoList.Add(simuToolInfoEntity);
                    }
                    if (getConfigService.ConfigGYBDCTool(CANTotalInfos, ref simuAppInfos))
                    {
                        // 添加高压不对称模拟工具到列表
                        int i = 0;
                        foreach (var simuAppInfo in simuAppInfos)
                        {
                            i++;
                            SimuToolInfoEntity simuToolInfoEntity = new SimuToolInfoEntity(SimuToolInfoList.Count + 1, "高压不对称模拟" + i.ToString(), simuAppInfo.SimuAPPName, simuAppInfo.SimuAPPPath, true, false);
                            SimuToolInfoList.Add(simuToolInfoEntity);
                        }
                    }


                }
            }

        }
    }
}
